package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnCollectPostBean;
import com.example.zhunongzonghe.beans.ZnExchangePostBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnCollectPost;
import com.example.zhunongzonghe.service.ZnCollectPostService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

@RestController
@RequestMapping("ZnCollectPost")
public class ZnCollectPostController {
    @Resource
    private ZnCollectPostService znCollectPostService;

    /**
     * 用户收藏帖子
     * @param znCollectPostBean
     * @return
     */
    @RequestMapping("/postCollect")
    public Result postAddCollect(@RequestBody ZnCollectPostBean znCollectPostBean, Principal principal){
        System.out.println("11111"+ znCollectPostBean);
        if (znCollectPostBean==null) return new Result(false,"数据异常",400);
        znCollectPostService.addCollect(znCollectPostBean,principal.getName());
        return new Result(true,"收藏成功",200);
    }

//    /**
//     * 分页查询当前用户的帖子收藏
//     * @param principal
//     * @param currentPage
//     * @param pageSize
//     * @return
//     */
//    @GetMapping("/getMyCollects")
//    public Result getMyCollects(Principal principal,
//                                @RequestParam("currentPage") Integer currentPage,
//                                @RequestParam("pageSize") Integer pageSize){
//        Page<ZnCollectPostBean> collectPostBeanPage =
//                znCollectPostService.getMyCollect(principal.getName(),currentPage,pageSize);
//        return new Result(true,"成功",200,collectPostBeanPage);
//    }
    /**
     * 分页查询当前用户的帖子收藏
     * @param principal
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getMyCollects")
    public Result getMyCollects(Principal principal,
                                @RequestParam("currentPage") Integer currentPage,
                                @RequestParam("pageSize") Integer pageSize){
        Page<ZnExchangePostBean> collectPostBeanPage =
                znCollectPostService.MyCollects(principal.getName(),currentPage,pageSize);
        return new Result(true,"成功",200,collectPostBeanPage);
    }

    /**
     * 用户取消收藏
     * @param znCollectPost
     * @return
     */
    @PutMapping("/cancelCollect")
    public Result putCancel(@RequestBody ZnCollectPost znCollectPost){
        System.out.println("收藏状态为==="+znCollectPost);
        if (znCollectPost==null) return new Result(false,"数据异常",400);
        znCollectPostService.updateStatus(znCollectPost);
        return new Result(true,"成功",200);
    }

}
